#spiral matrix

from functools import reduce

def get_empty_matrix(n,m=None):
	'''generate empty matrix of n column and m row'''
	if not m: m = n
	return matrix([[0 for column in range(n)] for row in range(m)])

def fill_spiral(matrix):
	n = matrix.height * matrix.width  
	i, j = 0, 0
	for p in range(int((matrix.width+1)/2)):

		#sx-dx
		for i in range(p, matrix.width-p):
			print("{0} - {1},{2}".format(n,i,p))
			matrix.array[p][i]=n
			n -= 1
		#up-bottom
		for j in range(p+1, matrix.height-p):
			print("{0} - {1},{2}".format(n,i,j))
			matrix.array[j][i]=n
			n -= 1

		#dx-sx
		for i in range(j-1, p-1, -1):
			print("{0} - {1},{2}".format(n,i,j))
			matrix.array[j][i]=n
			n -= 1

		#bottom-up
		for j in range(j-1, p, -1):
			print("{0} - {1},{2}".format(n,i,j))
			matrix.array[j][i]=n
			n -= 1

class matrix():
	array = []

	def __init__(self,array=[]):
		self.array = array
	
	def __str__(self):
		string = ""
		for row in self.array: string += str(row) + "\n"
		return string

	def get_width(self):
		return len(self.array)
	def get_height(self):
		return len(self.array[0])
	width = property(get_width, None, None)
	height = property(get_height, None, None)

if __name__ == "__main__":
	matrix = get_empty_matrix(1001)
	print(matrix)
	fill_spiral(matrix)
	print(matrix)
	diagonals = []
	for i in range(matrix.height):
		diagonals.append( matrix.array[i][i] )
		if i != matrix.height//2:
			diagonals.append( matrix.array[i][matrix.width-1-i] )
	print( reduce(lambda x,y: x+y, diagonals)  )
	print(diagonals)
